package tgc.edu.tms.web.controller;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import tgc.edu.tms.custom.CommonController;
import tgc.edu.tms.entity.Curriculum;
import tgc.edu.tms.entity.Schoolyear;
import tgc.edu.tms.web.form.SchoolyearForm;
@Controller
@RequestMapping(value = "/dictionary/schoolyear")
public class SchoolyearController extends CommonController<Schoolyear, Integer, SchoolyearForm>{

	@Override
	public Specification<Schoolyear> buildSpec(SchoolyearForm form) {
		Specification<Schoolyear> spec = new Specification<Schoolyear>() {

			@Override
			public Predicate toPredicate(Root<Schoolyear> root, CriteriaQuery<?> query,
					CriteriaBuilder criteriaBuilder) {
				List<Predicate> rules = new ArrayList<>();
				if (StringUtils.hasText(form.getSearch())) {
					Predicate like = criteriaBuilder.like(root.get("xnxq"), "%" + form.getSearch() + "%");
					rules.add(like);
				}
				return criteriaBuilder.and(rules.toArray(new Predicate[rules.size()]));
			}
			
		};
		return spec;
	}

}
